package com.home766.controller.terminal;

import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.shop.BaseController;
import net.biz.entity.Article;
import net.biz.entity.ArticleCategory;
import net.biz.service.ArticleCategoryService;
import net.biz.service.ArticleService;
import net.biz.util.ParamsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
@RequestMapping("/terminal/article")
public class TerminalArticleController extends BaseController {

	
	@Resource(name="articleServiceImpl")
	private ArticleService articleService;
	@Resource(name="articleCategoryServiceImpl")
	private ArticleCategoryService articleCategoryService;
	
	/**
	 * 文章列表查询
	 * @param model
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(ModelMap model, Pageable pageable, Long articleCategoryId){
		pageable.setPageSize(12);
		ArticleCategory articleCategory = null;
		if(articleCategoryId != null){
			articleCategory = articleCategoryService.find(articleCategoryId);
		}
		Page<Article> page = articleService.findPage(articleCategory, null, pageable,null);
		model.addAttribute("page", page);
		articleCategory = articleCategoryService.find(ParamsUtil.ARTICLE_CATEGORY_ZIXUNZX);
		if(articleCategory != null){
			model.addAttribute("articleCategorys", articleCategoryService.findChildren(articleCategory));			
		}
		model.addAttribute("articleCategoryId", articleCategoryId);
		return "/terminal/article/list";
	}
	
	/**
	 * 文件详情信息查看
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/content",method = RequestMethod.GET)
	public String content(ModelMap model, Long id){
		if(id == null){
			return TERMINAL_ERROR_VIEW;
		}
		Article article = articleService.find(id);
		if(article == null){
			return TERMINAL_ERROR_VIEW;
		}
		model.addAttribute("article", article);
		ArticleCategory articleCategory = articleCategoryService.find(ParamsUtil.ARTICLE_CATEGORY_ZIXUNZX);
		if(articleCategory != null){
			model.addAttribute("articleCategorys", articleCategoryService.findChildren(articleCategory));			
		}
		return "/terminal/article/content";
	}
	
}
